package com.stars.services.impl.company;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.stars.common.ObjectCopyUtils;
import com.stars.common.base.BaseConstant;
import com.stars.common.base.ResultMsg;
import com.stars.common.tree.TreeJson;
import com.stars.dao.mapper.company.CompanyMapper;
import com.stars.entity.company.CompanyEntity;
import com.stars.services.company.CompanyConfigServices;
import com.stars.services.impl.BaseConfigServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dj
 * @version V1.0
 * @Title: 业务逻辑
 * @date 2019-09-30 13:00:05
 */
@Service
public class CompanyConfigServicesImpl extends BaseConfigServiceImpl<CompanyMapper, CompanyEntity> implements CompanyConfigServices {

    @Autowired
    private CompanyMapper companyMapper;

    /**
     * 添加菜单数据
     *
     * @param command
     * @param map
     * @throws Exception
     */
    public void add(TreeJson command, Map<String, String> map) throws Exception {
        Gson gson = new Gson();
        if (StringUtils.isBlank(command.getAddJson())) {
            return;
        }
        List<CompanyEntity> addList = gson.fromJson(command.getAddJson(), new TypeToken<List<CompanyEntity>>() {
        }.getType());
        for (CompanyEntity add : addList) {
            //不包含Ext情况不往下走
            if (add.getPid().indexOf("Ext") == -1) {
                CompanyEntity companyEntity = toAddEntity(command, add);
                if (StringUtils.isNotBlank(companyEntity.getCompanyName())) {
                    insert(companyEntity);
                }
                map.put(add.getId(), companyEntity.getId());
                addParentId(add, companyEntity.getId(), addList, command, map);
            }
        }
    }

    /**
     * 递归添加树形
     *
     * @param addList
     * @param command
     */
    private void addParentId(CompanyEntity root, String parentId, List<CompanyEntity> addList, TreeJson command, Map<String, String> map) throws Exception {
        for (CompanyEntity c : addList) {
            if (root.getId().equals(c.getPid())) {
                CompanyEntity cc = toAddEntity(command, c);
                cc.setPid(parentId);
                if (StringUtils.isNotBlank(cc.getCompanyName())) {
                    insert(cc);
                }
                map.put(c.getId(), cc.getId());
                addParentId(c, cc.getId(), addList, command, map);
            }
        }
    }

    private CompanyEntity toAddEntity(TreeJson command, CompanyEntity add) {
        CompanyEntity companyEntity = new CompanyEntity();
        ObjectCopyUtils.copyPropertiesIf(add, companyEntity, BaseConstant.EXCLUDES);
        companyEntity.setCreateId(command.getCreateId());
        companyEntity.setCreateName(command.getCreateName());
        return companyEntity;
    }

    /**
     * 更新菜单数据
     *
     * @param command
     * @param map
     * @throws Exception
     */
    public void update(TreeJson command, Map<String, String> map) throws Exception {
        Gson gson = new Gson();
        if (StringUtils.isBlank(command.getUpdateJson())) {
            return;
        }
        List<CompanyEntity> updateList = gson.fromJson(command.getUpdateJson(), new TypeToken<List<CompanyEntity>>() {
        }.getType());
        for (CompanyEntity updateTree : updateList) {
            CompanyEntity companyEntity = companyMapper.selectByPrimaryKey(updateTree.getId());
            if (null == companyEntity) {
                return;
            }
            ObjectCopyUtils.copyPropertiesIf(updateTree, companyEntity, BaseConstant.EXCLUDES);
            companyEntity.setLastUpdateId(command.getCreateId());
            companyEntity.setLastUpdateName(command.getCreateName());
            companyEntity.setLastUpdateDate(new Date());
            //如果包含ext ，不往下走
            if (StringUtils.isNotBlank(updateTree.getPid()) && updateTree.getPid().indexOf("Ext") == 0) {
                //如果包含
                companyEntity.setPid(map.get(updateTree.getPid()));
            }
            if (StringUtils.isNotBlank(companyEntity.getCompanyName())) {
                updateSelectiveById(companyEntity);
            }
        }
    }
}